class PostController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [postInstanceList: Post.list(params), postInstanceTotal: Post.count()]
    }

    def create = {
        def postInstance = new Post()
        postInstance.properties = params
        return [postInstance: postInstance]
    }

    def save = {
        def postInstance = new Post(params)
        if (!postInstance.hasErrors() && postInstance.save()) {
            flash.message = "post.created"
            flash.args = [postInstance.id]
            flash.defaultMessage = "Post ${postInstance.id} created"
            redirect(action: "show", id: postInstance.id)
        }
        else {
            render(view: "create", model: [postInstance: postInstance])
        }
    }

    def show = {
        def postInstance = Post.get(params.id)
        if (!postInstance) {
            flash.message = "post.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Post not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [postInstance: postInstance]
        }
    }

    def edit = {
        def postInstance = Post.get(params.id)
        if (!postInstance) {
            flash.message = "post.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Post not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [postInstance: postInstance]
        }
    }

    def update = {
        def postInstance = Post.get(params.id)
        if (postInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (postInstance.version > version) {
                    
                    postInstance.errors.rejectValue("version", "post.optimistic.locking.failure", "Another user has updated this Post while you were editing")
                    render(view: "edit", model: [postInstance: postInstance])
                    return
                }
            }
            postInstance.properties = params
            if (!postInstance.hasErrors() && postInstance.save()) {
                flash.message = "post.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Post ${params.id} updated"
                redirect(action: "show", id: postInstance.id)
            }
            else {
                render(view: "edit", model: [postInstance: postInstance])
            }
        }
        else {
            flash.message = "post.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Post not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def postInstance = Post.get(params.id)
        if (postInstance) {
            try {
                postInstance.delete()
                flash.message = "post.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Post ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "post.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Post ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "post.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Post not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
